package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityEditeCarMoreInfoBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.AllFunction;
import com.sgb.kjwl.model.entity.MyCarInfoEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.PickerHelper;
import com.sgb.kjwl.utils.RangeFocusChangeListener;
import com.sgb.kjwl.utils.VehicleConstant;
import com.sgb.kjwl.view.ui.adapter.transport.CarFuncationPopWindow;
import com.sgb.kjwl.viewmodel.self.EditeCarMoreModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.CommonPickerDialog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

/**
 * @Author : 熊丽
 * @date :2021/1/20 0020$ 11:00$
 * @Description :编辑车辆更多信息:栏高/是否自卸$
 */
public class EditeCarMoreInfoActivity extends AppActivity  {
    private ActivityEditeCarMoreInfoBinding mBinding;
    @Inject
    EditeCarMoreModel mMoreModel;
    MyCarInfoEntity myCarInfo;
    private List<AllFunction> mlist ;
    CarFuncationPopWindow funcationPopWindow;
    StringBuilder sb,no;
    @Override
    protected void initView() {
        super.initView();
         myCarInfo = (MyCarInfoEntity) getIntent().getSerializableExtra("myCarInfo");
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_edite_car_more_info);
        mBinding.include.titleView.setText("编辑车辆更多信息");
        mBinding.include.ivBack.setOnClickListener(this);
        mBinding.iitIsunload.setOnWholeItemClickListener(this::onClick);
        mBinding.tvCommit.setOnClickListener(this::onClick);
        mBinding.carFunction.setOnWholeItemClickListener(this::onClick);
        mBinding.edtHeight.setOnFocusChangeListener(new RangeFocusChangeListener(0.01, 999.99));
        funcationPopWindow = new CarFuncationPopWindow(this);
        funcationPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                List<AllFunction> entity = (List<AllFunction>) key1;
                setMlist(entity);
            }
        });
        mMoreModel.getCarFuncation();
    }

    @Override
    protected void init() {
        if (myCarInfo==null)return;
        mBinding.iitIsunload.setContent(VehicleConstant.getIsDumpStr(myCarInfo.getIsDump()));
        mBinding.carFunction.setContent(myCarInfo.getFunctionNos());
        if (TextUtils.isEmpty(myCarInfo.getHeightHurdle()))return;
            mBinding.edtHeight.setText(myCarInfo.getHeightHurdle());
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iit_isunload:
                showPeoplePop();
                break;
            case R.id.tv_commit:
                if (!TextUtils.isEmpty(mBinding.edtHeight.getText().toString())&&NumberUtil.convertToFloat(mBinding.edtHeight.getText().toString())>999.99){
                    mBinding.edtHeight.setText("999.99");
                }
                if (!TextUtils.isEmpty(mBinding.edtHeight.getText().toString())&&NumberUtil.convertToFloat(mBinding.edtHeight.getText().toString())<0.01){
                    mBinding.edtHeight.setText("0.01");
                }
                if (TextUtils.isEmpty(mBinding.carFunction.getContent())){
                    MToast.showToast(this,"请选择车辆功能");
                    return;
                }
                    mMoreModel.editHurdleHeightAndDumper(this,myCarInfo.getId(),mBinding.edtHeight.getText().toString(),"自卸".equals(mBinding.iitIsunload.getContent())?1:"非自卸".equals(mBinding.iitIsunload.getContent())?0:-1,no.substring(0,no.length() - 1));
                break;
            case R.id.car_function:
                if (mlist==null){
                    return;
                }
                funcationPopWindow.showDownPopwindow(mBinding.carFunction, true);
                funcationPopWindow.backgroundAlpha(0.5f);
                funcationPopWindow.setData(mlist);
                break;
        }
    }
    //车辆选择器
    private void showPeoplePop() {
        List<String> unloadlist = Arrays.asList("自卸", "非自卸");
        OptionsPickerView businessOption;
        businessOption = PickerHelper.with(this).getCommonPicker("是否自卸", unloadlist, (options1, options2, options3, v) -> {
            String content = unloadlist.get(options1 == 0 ? 1 : 0);
            mBinding.iitIsunload.setContent(content);
        });
        businessOption.show();
//        OptionsPickerView businessOption = new CommonPickerDialog(this, (options1, options2, options3, v) -> {
//            String content = unloadlist.get(options1);
//            mBinding.iitIsunload.setContent(content);
//        }).build();
//        businessOption.setPicker(unloadlist);
//        businessOption.setSelectOptions(0);
//        businessOption.setTitleText("是否自卸");
//        businessOption.show();
    }
    public void setData(){
        MToast.showToast(this,"保存成功");
        setResult(RESULT_OK);
        finish();
    }
    public void setMyCarList(List<AllFunction> list){
        mlist = new ArrayList<>();
        if (!TextUtils.isEmpty(myCarInfo.getFunctionNo())){
            String[] strings = myCarInfo.getFunctionNo().split(",");
            for (int i =0;i<list.size();i++){
                for (int j = 0;j<strings.length;j++){
                    if (list.get(i).getNo().equals(strings[j])){
                        list.get(i).setIsselect(true);
                    }
                }
            }
        }
        this.mlist = list;
        setMlist(mlist);
    }
    public void setMlist(List<AllFunction> entity){
        sb= new StringBuilder();
        no = new StringBuilder();
        if (entity==null) {
            return;
        }
        for (int i=0;i<entity.size();i++){
            if (entity.get(i).isIsselect()){
                String string=  entity.get(i).getName();
                String nosub = entity.get(i).getNo();
                no.append(nosub);
                no.append(",");
                sb.append(string);
                sb.append(",");
            }
        }
        if (sb.length()==0){
            mBinding.carFunction.setContent("");
        }else {
            mBinding.carFunction.setContent(sb.substring(0,sb.length() - 1));
        }
    }
    }
